package com.block.datapush.common.filter.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.block.common.enums.BaseTypeInfoEnum;
import com.block.common.service.LogFactory;
import com.block.datapush.common.enums.FilterErrorEnum;
import com.block.datapush.common.filter.IFilterHandlerService;
import com.block.datapush.common.model.param.FilterHandlerParam;
import com.block.datapush.common.model.result.FilterHandlerResult;
import com.block.db.entity.MemberExtInfo;
import org.slf4j.Logger;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

@Service
public class FilterOrFilterHandlerService implements IFilterHandlerService {

    private static Logger log = LogFactory.getPushLog();

    @Override
    public FilterHandlerResult handlerFilterLogic(FilterHandlerParam handlerParam, MemberExtInfo memberExtInfo) {

        String filterValue = handlerParam.getOrFilter();
        // 任选一条件内容为空，则返回筛选成功
        if (StrUtil.isEmpty(filterValue)){
            return FilterHandlerResult.success();
        }

        // 任选一 数组为空，则返回筛选成功
        List<String> valueList = JSON.parseArray(filterValue, String.class);
        if (CollUtil.isEmpty(valueList)){
            return FilterHandlerResult.success();
        }

        // 添加用户的条件
        List<String> userValueList = new ArrayList<>();
        userValueList.add(memberExtInfo.getZhimaScore());
        userValueList.add(memberExtInfo.getHouse());
        userValueList.add(memberExtInfo.getCar());
        userValueList.add(memberExtInfo.getGjj());
        userValueList.add( memberExtInfo.getYuqi());
        userValueList.add(memberExtInfo.getBaodan());
        userValueList.add(memberExtInfo.getShebao());

        // 得到用户条件的任选一  数据
        List<String> afterList = handleUserSource(userValueList);

        // 获取资质的交集
        Collection<String> resultList = CollUtil.intersection(afterList, valueList);
        if (CollUtil.isNotEmpty(resultList)){
            // 任选一不为空，满足条件，
            return FilterHandlerResult.success();
        }

        String message = StrUtil.format("【resultList】{}【valueList】{}【userValueList】{}",
                JSON.toJSONString(afterList), JSON.toJSONString(valueList), JSON.toJSONString(userValueList));
        log.info("【机构条件判断】false【任选一】{}", message);
        return FilterHandlerResult.error(FilterErrorEnum.ORFILTER_ERROR, message);
    }


    /**
     * 处理用户自制信息，获取到任选一条件的值
     */
    public static List<String> handleUserSource(List<String> userValueList){

        List<String> resultList = new ArrayList<>();
        for (String valueInfo : userValueList) {

            // 字符串切割
            int index = valueInfo.length() - 3;

            String pre = StrUtil.subPre(valueInfo, index);
            String suf = StrUtil.subSuf(valueInfo, index);

            if (StrUtil.equals("yq", pre)){
                if (StrUtil.equals(suf, "001")){
                    resultList.add(pre + "000");
                }
            }else{
                if (!StrUtil.equals(suf, "001")){
                    resultList.add(pre + "000");
                }
            }
        }
        return resultList;
    }


    public static void main(String[] args) {

        List<String> resultList = new ArrayList<>();
        resultList.add("zm001");
        resultList.add("car002");
        resultList.add("hu005");
        resultList.add("gjj001");
        resultList.add("yq002");
        resultList.add("bd003");
        resultList.add("sb001");

        List<String> stringList = handleUserSource(resultList);
        System.out.println(JSON.toJSONString(stringList));

    }

}
